package com.hziee.cosmanage.companymanage.service.impl;

import com.hziee.cosmanage.companymanage.entity.CharacterEntity;
import com.hziee.cosmanage.companymanage.service.CharacterService;
import com.hziee.cosmanage.companymanage.vo.PartAddVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.common.utils.PageUtils;
import com.hziee.common.utils.Query;

import com.hziee.cosmanage.companymanage.dao.PartDao;
import com.hziee.cosmanage.companymanage.entity.PartEntity;
import com.hziee.cosmanage.companymanage.service.PartService;


@Service("partService")
public class PartServiceImpl extends ServiceImpl<PartDao, PartEntity> implements PartService {

    @Autowired
    CharacterService characterService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PartEntity> page = this.page(
                new Query<PartEntity>().getPage(params),
                new QueryWrapper<PartEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addPart(PartAddVo partAddVo) {
        Integer pid = partAddVo.getPid();
        //如果父节点为0说明是一个大部门，直接添加
        if (pid==0){
            PartEntity partEntity = new PartEntity();
            BeanUtils.copyProperties(partAddVo,partEntity);
            partEntity.setPartUserId(-1);
            partEntity.setType(1);
            this.save(partEntity);
        }else {
            //如果父节点不是0说明是一个部门下面的子部门
            PartEntity partEntity = new PartEntity();
            BeanUtils.copyProperties(partAddVo,partEntity);
            partEntity.setPartUserId(-1);
            PartEntity parentEntity = this.getOne(new QueryWrapper<PartEntity>().eq("company_part_id", pid));
            partEntity.setSort(parentEntity.getSort()+1);
            partEntity.setStatus(1);
            partEntity.setType(2);
            this.save(partEntity);
        }

    }

    @Override
    public List<PartEntity> listWithTree(Integer cId) {
        //1.查出所有部门
        List<PartEntity> entities = this.list();
        //2.找到所有一级部门（大部门）
        List<PartEntity> collect = entities.stream().filter(partEntity ->
                partEntity.getPid() == 0
        ).map(part -> {
            part.setChildren(getChildrens(part, entities));
            return part;
        }).sorted((menu1, menu2) -> {
            return menu1.getSort() - menu2.getSort();
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public CharacterEntity getCompanyCharacterName(Integer cId, Integer characterId) {
        CharacterEntity one = characterService.getOne(new QueryWrapper<CharacterEntity>().eq("company_id", cId).eq("company_character_id", characterId));
        return one;
    }

    @Override
    public PartEntity getCompanyPartName(Integer cId, Integer partId) {
        PartEntity one = this.getOne(new QueryWrapper<PartEntity>().eq("company_id", cId).eq("company_part_id", partId));
        return one;
    }

    //递归查找所有菜单的子菜单
    private List<PartEntity> getChildrens(PartEntity root, List<PartEntity> all) {

        List<PartEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getPid() == root.getCompanyPartId();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}